package com.linkage.educloud.ah.view.activity.topic;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.topic.TopicChildDetailCommentAdapter;
import com.linkage.educloud.ah.adapter.topic.TopicDetailLabelAdapter;
import com.linkage.educloud.ah.bean.BannerBean;
import com.linkage.educloud.ah.bean.PicItem;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.mvp.contract.topic.ITopicDetailContract;
import com.linkage.educloud.ah.mvp.presenter.topic.TopicDetailPresenter;
import com.linkage.educloud.ah.util.ShareUtils;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.linkage.educloud.ah.view.activity.big_picture.BigPictureActivity;
import com.linkage.educloud.ah.view.activity.classspace.DynamicDetailActivity;
import com.linkage.educloud.ah.widget.BannerM;
import com.linkage.educloud.ah.widget.CommonPopWindow;
import com.linkage.educloud.ah.widget.ExpressionUtils;
import com.linkage.educloud.ah.widget.NoScrollGridView;
import com.linkage.educloud.ah.widget.NoScrollListView;
import com.linkage.educloud.ah.widget.PanelView;
import com.zzhou.common.library.util.ToastShow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/11/7.
 */

public class TopicChildDetailActivity extends BasicTitleActivity implements View.OnClickListener, ITopicDetailContract.IView, CommonPopWindow.ViewClickListener {

    @BindView(R.id.ac_topicdetail_scroll)
    ScrollView ac_topicdetail_scroll;
    @BindView(R.id.ac_topicdetail_cannot_comment)
    ImageView ac_topicdetail_cannot_comment;
    @BindView(R.id.ac_topicdetail_title)
    TextView ac_topicdetail_title;
    @BindView(R.id.ac_topicdetail_nickname)
    TextView ac_topicdetail_nickname;
    @BindView(R.id.ac_topicdetail_time)
    TextView ac_topicdetail_time;
    @BindView(R.id.ac_topicdetail_priase_num)
    TextView ac_topicdetail_priase_num;
    @BindView(R.id.ac_topicdetail_read_num)
    TextView ac_topicdetail_read_num;
    @BindView(R.id.ac_topicdetail_img)
    BannerM ac_topicdetail_img;
    @BindView(R.id.ac_topicdetail_content)
    TextView ac_topicdetail_content;
    @BindView(R.id.ac_topicdetail_zan)
    LinearLayout ac_topicdetail_zan;
    @BindView(R.id.ac_topicdetail_zan_text)
    TextView ac_topicdetail_zan_text;
    @BindView(R.id.ac_topicdetail_share)
    LinearLayout ac_topicdetail_share;
    @BindView(R.id.ac_topicdetail_label)
    LinearLayout ac_topicdetail_label;
    @BindView(R.id.ac_topicdetail_label_grid)
    NoScrollGridView ac_topicdetail_label_grid;
    @BindView(R.id.ac_topicdetail_comment_list)
    NoScrollListView ac_topicdetail_comment_list;
    @BindView(R.id.ac_topicdetail_comment)
    LinearLayout ac_topicdetail_comment;
    @BindView(R.id.ac_topicdetail_comment_text)
    TextView ac_topicdetail_comment_text;
    @BindView(R.id.ac_topicdetail_comment_ed)
    EditText ac_topicdetail_comment_ed;
    @BindView(R.id.ac_topicdetail_bottom_lin)
    LinearLayout ac_topicdetail_bottom_lin;
    @BindView(R.id.ac_topicdetail_face)
    ImageView ac_topicdetail_face;
    @BindView(R.id.ac_topicdetail_push)
    TextView ac_topicdetail_push;
    @BindView(R.id.ac_topic_child_detail_panel)
    PanelView ac_topic_child_detail_panel;

    private final static String KEY_ID = "id";

    ITopicDetailContract.IPresenter iPresenter;
    int is_visibility = 1;//表情是否展示中
    TopicChildBean childBean = new TopicChildBean();//话题对象
    TopicChildDetailCommentAdapter adapter;

    public static void launch(Context context, String id) {
        Intent intent = new Intent(context, TopicChildDetailActivity.class);
        intent.putExtra(KEY_ID, id);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_topic_child_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iPresenter = new TopicDetailPresenter(this, this);
        setTitle("详情");
        showZloading();
        iPresenter.getCardDetail(getIntent().getStringExtra(KEY_ID));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_topicdetail_zan:
                showZloading();
                iPresenter.pushZan(childBean.getId(), 1);
                break;
            case R.id.ac_topicdetail_share:
                ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.black));
//                mWindow.setBackgroundDrawable(dw);
                CommonPopWindow.newBuilder()
                        .setView(R.layout.popupwindow_share)
                        .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setViewOnClickListener(this)
                        .setBackgroundDrawable(dw)
                        .setBackgroundDarkEnable(true)
                        .build(this)
                        .showAsBottom(ac_topicdetail_share);
                break;
            case R.id.ac_topicdetail_comment_text:
                ac_topicdetail_comment_text.setVisibility(View.GONE);
                ac_topicdetail_comment_ed.setVisibility(View.VISIBLE);
                ac_topicdetail_bottom_lin.setVisibility(View.VISIBLE);
                break;
            case R.id.ac_topicdetail_face:
                if (is_visibility == 1) {
                    ac_topicdetail_face.setImageResource(R.mipmap.ic_btn_face_on);
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
                    //延时0.3秒 以解决表情栏展示比隐藏输入法快,界面被顶上去的bug（目前没想到其他好方法）
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ac_topic_child_detail_panel.setVisibility(View.VISIBLE);
                        }
                    }, 300);
                    is_visibility = 0;
                } else if (is_visibility == 0) {
                    ac_topicdetail_face.setImageResource(R.mipmap.ic_btn_face_off);
                    ac_topic_child_detail_panel.setVisibility(View.GONE);
                    is_visibility = 1;
                }
                break;
            case R.id.ac_topicdetail_push:
                showZloading();
                iPresenter.pushComment(childBean.getId(), ac_topicdetail_comment_ed.getText().toString().trim());
                break;
        }
    }

    //暴露给表情组件调用 用于点了输入框改变表情状态
    public void change() {
        ac_topicdetail_face.setImageResource(R.mipmap.ic_btn_face_off);
        ac_topic_child_detail_panel.setVisibility(View.GONE);
        is_visibility = 1;
    }

    public void showData(TopicChildBean bean) {
        childBean = bean;
        iPresenter.getComments(getIntent().getStringExtra(KEY_ID));
        ac_topicdetail_scroll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                change();
                ac_topicdetail_comment_text.setVisibility(View.VISIBLE);
                ac_topicdetail_comment_ed.setVisibility(View.GONE);
                ac_topicdetail_bottom_lin.setVisibility(View.GONE);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(ac_topicdetail_comment_text.getWindowToken(), 0);
                return false;
            }
        });
        ac_topic_child_detail_panel.installView(2, 2, ac_topicdetail_comment_ed);
        ac_topicdetail_zan.setOnClickListener(this);
        ac_topicdetail_share.setOnClickListener(this);
        ac_topicdetail_comment_text.setOnClickListener(this);
        ac_topicdetail_face.setOnClickListener(this);
        ac_topicdetail_push.setOnClickListener(this);
        if (bean.getPermit_comment() != 1) {//禁止评论
            ac_topicdetail_cannot_comment.setVisibility(View.VISIBLE);
            ac_topicdetail_comment.setVisibility(View.GONE);
        } else {
            ac_topicdetail_cannot_comment.setVisibility(View.GONE);
            ac_topicdetail_comment.setVisibility(View.VISIBLE);
        }
        ac_topicdetail_title.setText(bean.getName());
        ac_topicdetail_nickname.setText(bean.getNick());
        ac_topicdetail_time.setText(bean.getPublish_time());
        ac_topicdetail_priase_num.setText(String.valueOf(bean.getZan_num()));
        ac_topicdetail_read_num.setText(String.valueOf(bean.getRead_num()));
        if (!bean.getImg().isEmpty()) {//图片viewpager
            ac_topicdetail_img.setVisibility(View.VISIBLE);
            String[] imgs = bean.getImg().split(";");
            //banner数据
            List<BannerBean> list = new ArrayList<>();
            for (int a = 0; a < imgs.length; a++) {
                BannerBean bannerBean = new BannerBean();
                bannerBean.setUrl(imgs[a]);
                list.add(bannerBean);
            }
            //查看大图数据
            final ArrayList<PicItem> picList = new ArrayList<PicItem>();
            for (int i = 0; i < list.size(); i++) {
                PicItem it = new PicItem(list.get(i).getUrl(), i);
                picList.add(it);
            }
            ac_topicdetail_img.setBannerBeanList(list);
            ac_topicdetail_img.setOnItemClickListener(new BannerM.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    BigPictureActivity.launch(TopicChildDetailActivity.this, picList, position);
                }
            });
            ac_topicdetail_img.show();
        } else {
            ac_topicdetail_img.setVisibility(View.GONE);
        }
        ac_topicdetail_content.setText(ExpressionUtils.replaceFace(this, bean.getContent()));
        ac_topicdetail_zan_text.setText("喜欢(" + String.valueOf(bean.getZan_num()) + ")");
        if (!bean.getLabel().isEmpty()) {//标签区域
            ac_topicdetail_label.setVisibility(View.VISIBLE);
            String[] labels = bean.getLabel().split(",");
            TopicDetailLabelAdapter adapter = new TopicDetailLabelAdapter(this, labels);
            ac_topicdetail_label_grid.setAdapter(adapter);
        } else {
            ac_topicdetail_label.setVisibility(View.GONE);
        }
    }

    public void showCommentDate(final List<TopicChildBean> list) {
        hideZloading();
        adapter = new TopicChildDetailCommentAdapter(this, list);
        adapter.setOnitemClickListener(new TopicChildDetailCommentAdapter.OnitemClickListener() {
            @Override
            public void onItemClick(int position) {
                showZloading();
                iPresenter.pushZan(list.get(position).getId(), 2);
            }
        });
        ac_topicdetail_comment_list.setAdapter(adapter);
    }

    public void changePriase(int state) {
        hideZloading();
        if (state == 1) {//点赞话题
            if (childBean.getIs_zan() == 1) {
                childBean.setIs_zan(0);
                childBean.setZan_num(childBean.getZan_num() - 1);
                ac_topicdetail_priase_num.setText(String.valueOf(childBean.getZan_num()));
                ac_topicdetail_zan_text.setText("喜欢(" + String.valueOf(childBean.getZan_num()) + ")");
            } else {
                childBean.setIs_zan(1);
                childBean.setZan_num(childBean.getZan_num() + 1);
                ac_topicdetail_priase_num.setText(String.valueOf(childBean.getZan_num()));
                ac_topicdetail_zan_text.setText("喜欢(" + String.valueOf(childBean.getZan_num()) + ")");
            }
        } else {//点赞评论
            iPresenter.getComments(getIntent().getStringExtra(KEY_ID));

            ac_topicdetail_comment_text.setVisibility(View.VISIBLE);
            ac_topicdetail_comment_ed.setVisibility(View.GONE);
            ac_topicdetail_bottom_lin.setVisibility(View.GONE);
            ac_topic_child_detail_panel.setVisibility(View.GONE);
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public void hidezloading() {
        hideZloading();
    }

    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.popupwindow_share:
                LinearLayout popup_share_wechat = view.findViewById(R.id.popup_share_wechat);
                LinearLayout popup_share_circle = view.findViewById(R.id.popup_share_circle);
                LinearLayout popup_share_qq = view.findViewById(R.id.popup_share_qq);
                Button popup_share_cancel = view.findViewById(R.id.popup_share_cancel);
                popup_share_wechat.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ShareUtils.share_wechat(TopicChildDetailActivity.this, childBean.getAddress(), "", childBean.getName(), childBean.getContent());
                        mPopupWindow.dismiss();
                    }
                });
                popup_share_circle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ShareUtils.share_circle(TopicChildDetailActivity.this, childBean.getAddress(), "", childBean.getName(), childBean.getContent());
                        mPopupWindow.dismiss();
                    }
                });
                popup_share_qq.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ShareUtils.share_qq(TopicChildDetailActivity.this, childBean.getAddress(), "", childBean.getName(), childBean.getContent());
                        mPopupWindow.dismiss();
                    }
                });
                popup_share_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                break;
        }
    }
}
